﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBUtility;
using DAL = AccessDAL;

namespace BLL
{
    public class GBook
    {
        DAL.GBookTableAdapters.GBookTableAdapter DGB =
            new DAL.GBookTableAdapters.GBookTableAdapter();

        MODEL.GBook MGB = new MODEL.GBook();
        MODEL.GBook _MGB = new MODEL.GBook();

        #region GetGBookByID
        /// <summary>
        /// GetGBookByID
        /// </summary>
        /// <param name="MGB"></param>
        /// <returns></returns>
        public MODEL.GBook GetGBookByID(MODEL.GBook MGB)
        {
            try
            {
                DataTable DT = DGB.GetDataByID(MGB.GBook_ID);

                _MGB.GBook_Content = DT.Rows[0]["GBook_Content"].ToString();
                _MGB.GBook_DateTime = Convert.ToDateTime(DT.Rows[0]["GBook_DateTime"]);
                _MGB.GBook_Email = DT.Rows[0]["GBook_Email"].ToString();
                _MGB.GBook_ID = Convert.ToInt32(DT.Rows[0]["GBook_ID"]);
                _MGB.GBook_IP = DT.Rows[0]["GBook_IP"].ToString();
                _MGB.GBook_Name = DT.Rows[0]["GBook_Name"].ToString();
                _MGB.GBook_QQ = DT.Rows[0]["GBook_QQ"].ToString();
                _MGB.GBook_Reply = DT.Rows[0]["GBook_Reply"].ToString();
                _MGB.GBook_State = Convert.ToBoolean(DT.Rows[0]["GBook_State"]);
                _MGB.GBook_Telephone = DT.Rows[0]["GBook_Telephone"].ToString();
                _MGB.GBook_Title = DT.Rows[0]["GBook_Title"].ToString();

                return _MGB;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加留言
        /// <summary>
        /// 添加留言
        /// </summary>
        /// <param name="MGB"></param>
        public void Add(MODEL.GBook MGB)
        {
            try
            {
                DataTable DT = DGB.GetDataByIPOrderByDateTime(MGB.GBook_IP);

                if (DT.Rows.Count != 0)
                {
                    DateTime DT_LastGBook = Convert.ToDateTime(DT.Rows[0]["GBook_DateTime"]);
                    TimeSpan TS = MGB.GBook_DateTime - DT_LastGBook;
                    if (TS.Minutes < 2)
                    {
                        throw new Exception("相同IP两分钟内不可重复留言");
                    }
                }
                

                DGB.Add(MGB.GBook_Title, MGB.GBook_Name, MGB.GBook_Telephone, MGB.GBook_Email, MGB.GBook_QQ,
                    MGB.GBook_Content, MGB.GBook_State, MGB.GBook_Reply, MGB.GBook_IP, MGB.GBook_DateTime);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除留言
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="MGB"></param>
        public void Del(MODEL.GBook MGB)
        {
            try
            {
                DGB.Del(MGB.GBook_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 切换留言显示状态
        /// <summary>
        /// 切换留言显示状态
        /// </summary>
        /// <param name="MGB"></param>
        public void ModState(MODEL.GBook MGB)
        {
            try
            {
                string SQLstr = "UPDATE [GBook] SET [GBook_State]= NOT [GBook_State]";
                SQLstr += "WHERE [GBook_ID]=" + MGB.GBook_ID;

                AccHelper.ExecuteSQL(SQLstr);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 回复留言
        /// <summary>
        /// 回复留言
        /// </summary>
        /// <param name="MGB"></param>
        public void Reply(MODEL.GBook MGB)
        {
            try
            {
                DGB.ReplyByID(MGB.GBook_Reply, MGB.GBook_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
